<?php 
class ControllerContentList extends Controller {
	public function index(){	       			
        $this->language->load('content/content');
        
        $this->document->title = $this->language->get('meta_title');
        $this->document->keywords = $this->config->get('config_meta_keywords');
        $this->document->description = $this->config->get('config_meta_description');
        
        $this->data['template'] = $this->config->get('config_template');         
        $this->data['heading_title'] = $this->language->get('list_heading_title');
        
		$this->document->breadcrumbs = array();		
      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTP_SERVER . 'index.php?route=common/home',
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);		
		
        $limit = 6;
        $page  = isset($this->request->get['page']) ? $this->request->get['page'] : 1;      
        $this->data['contents'] = array();
        
        $this->load->model('content/category');
        $this->load->model('content/content');		   
        
        $categories = $this->model_content_category->getCategories(0, 0, 1);   
        reset($categories);	
        $category = current($categories);
        $contents = $this->model_content_content->getContentsByCategoryId($category['category_id'], 'p.date_modified', 'DESC', $limit, $page);
        
        //$content_total = $this->model_content_content->getTotalContent();
        $content_total = $this->model_content_content->getTotalContentByCategory($category['category_id']);
         
		if($contents){	
            $this->load->model('tool/image');
            $this->load->model('tool/seo_url');
            
            foreach($contents as $item){
                //$title = (strlen($item['name']) > 52) ? (mb_substr($item['name'], 0, 52) . ' ...') : $item['name'];
                $title = $item['name'];                
                //$image = $item['image'] ? $this->model_tool_image->resize($item['image'], 163, 88) : null;
                $image = $item['image'] ? $this->model_tool_image->resize($item['image'], 95, 88) : null; 
                
                $desc_info = html_entity_decode($item['description']);
                if(stripos($desc_info, '<hr />')){
                    $description = explode('<hr />', $desc_info);              
                    $description = strip_tags($description[0], '<a>') . '...';
                }
                elseif(stripos($desc_info, '{READ_MORE}')){
                    $description = explode('{READ_MORE}', $desc_info);
                    $description = strip_tags($description[0], '<a>') . '...';
                }
                else{
                    $description = (strlen($desc_info) > 396) ? (mb_substr(strip_tags($desc_info), 0, 400) . ' ...') : html_entity_decode($item['description']);    
                }
                                
                $this->data['contents'][] = array(
                    'title'         => $item['name'],
                    'short_title'   => $title,
                    'image'         => $image,
                    'text'          => $description,
                    'href'          => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=content/item&content_id=' . $item['content_id']),                    
                );           
            }
            
            $pagination = new Pagination();
			$pagination->total = $content_total;
			$pagination->page = $page;
			$pagination->limit = $limit; 
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=content/list&page={page}');			
			$this->data['pagination'] = $pagination->renderPaging();  
                
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/content/list.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/content/list.tpl';
			} 
            else {
				$this->template = 'default/template/content/list.tpl';
			}
			
			$this->children = array(
				'common/header',
				'common/footer',
				'common/column_left',				
			);		
			
	  		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    	} 
        else {      	
	  		$this->document->title = $this->language->get('list_heading_title');
            $this->data['heading_title'] = $this->language->get('list_heading_title');
      		$this->data['text_empty'] = $this->language->get('text_list_empty');

			if(file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/content/list.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/content/list.tpl';
			} 
            else{
				$this->template = 'default/template/content/list.tpl';
			}
			
			$this->children = array(
				'common/header',
				'common/footer',
				'common/column_left',				
			);	
		
	  		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    	}
  	}
}
?>
